/**
 * 判断是否是空对象
 *
 * @export
 *
 */
export function isNullObj(val) {
  return JSON.stringify(val) === "{}"
}
/**
 * 判断一个对象是否存在key
 * 如果传入第二个参数key，则是判断这个obj对象是否存在key这个属性
 * 如果没有传入key这个参数，则判断obj对象是否有键值对
 */
export const hasKey = (obj, key) => {
  if (key) {
    return key in obj
  } else {
    const keysArr = Object.keys(obj)
    return keysArr.length
  }
}

// 去除对象中null值、undefined值
export const removePropertyOfNull = (obj) => {
  if (Object.prototype.toString.call(obj) === "[object Object]") {
    Object.keys(obj).forEach((item) => {
      if (Object.prototype.toString.call(obj[item]) === "[object Object]") {
        // 深度删除
        this.removePropertyOfNull(obj[item])
      }
      if ((!obj[item] && obj[item] !== 0) || obj[item]?.length === 0) {
        delete obj[item]
      }
      if (obj[item]?.length > 0 && Array.isArray(obj[item])) {
        console.log("obj[item]?.length > 0", obj[item])
        const formatArr = obj[item].filter((i) => i)
        obj[item] = formatArr
      }
    })
    return obj
  } else {
    return {}
  }
}

/**
 * @param {String} url
 * @description 从URL中解析参数
 */
export const getParams = (url) => {
  if (url.indexOf("?") === -1) {
    return {}
  }
  const keyValueArr = url.split("?")[1].split("&")
  const paramObj = {}
  keyValueArr.forEach((item) => {
    const keyValue = item.split("=")
    paramObj[keyValue[0]] = keyValue[1]
  })
  return paramObj
}
/**
 * 去除空格
 * @param {*} str
 */
export function deletenone(str) {
  return str.replace(/\s+/g, "")
}
/**
 * 判断对象类型
 * @param [obj: any] 参数对象
 * @returns String
 */
export function isType(obj) {
  return Object.prototype.toString
    .call(obj)
    .replace(/^\[object (.+)\]$/, "$1")
    .toLowerCase()
}
/**
 * 判断是否非空
 * @param [object: any] 参数对象
 * @returns Boolean
 */
export function isNotEmpty(object) {
  const type = isType(object)
  if (type === "array") {
    return !!object.length
  } else if (type === "object") {
    return !!Object.keys(object).length
  } else if (type === "string") {
    return !!object.length
  } else if (type === "boolean") {
    return object
  } else {
    return false
  }
}
/**
 * 解析一个url并生成window.location对象中包含的域
 * location:
 * {
 *      href: '包含完整的url',
 *      origin: '包含协议到pathname之前的内容',
 *      protocol: 'url使用的协议，包含末尾的:',
 *      host: '完整主机名，包含:和端口',
 *      hostname: '主机名，不包含端口'
 *      port: '端口号',
 *      pathname: '服务器上访问资源的路径/开头',
 *      search: 'query string，?开头',
 *      hash: '#开头的fragment identifier'
 * }
 *
 * @param {string} url 需要解析的url
 * @return {Object} 包含url信息的对象
 */

export function parseUrlFn(url) {
  // let url = url || location.href;
  const result = {}
  const keys = [
    "href",
    "origin",
    "protocol",
    "host",
    "hostname",
    "port",
    "pathname",
    "search",
    "hash"
  ]
  var i
  var regexp = /(([^:]+:)\/\/(([^:\/\?#]+)(:\d+)?))(\/[^?#]*)?(\?[^#]*)?(#.*)?/

  var match = regexp.exec(url)
  //  console.info('match=', match);

  if (match) {
    for (i = keys.length - 1; i >= 0; --i) {
      result[keys[i]] = match[i] ? match[i] : ""
    }
  }
  if (result.search) {
    var queryObj = {}
    var reg = /[?&]([^=&#]+)=([^&#]*)/g
    var querys = result.search.match(reg)
    if (querys) {
      for (var j in querys) {
        var query = querys[j].split("=")
        var key = query[0].substr(1),
          value = query[1]
        queryObj[key]
          ? (queryObj[key] = [].concat(queryObj[key], value))
          : (queryObj[key] = value)
      }
    }
    result.search = queryObj
  }
  //  console.info('result=', result);
  return result
}
// 产生8位数字、字母随机数
export default function randUtils() {
  const source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  const letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  const number = "0123456789"
  const random = gPwd()
  return random

  function gPwd() {
    const range = generatePassword(6, source)
    const lettval = generatePassword(1, letter)
    const numval = generatePassword(1, number)
    const pwd = lettval + numval + range
    return pwd
  }
  function generatePassword(length, resource) {
    length = length || 32
    let s = ""
    for (let i = 0; i < length; i++) {
      s += resource.charAt(Math.ceil(Math.random() * 1000) % resource.length)
    }
    return s
  }
}
